package joney.main.views.report;

import java.util.Collections;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import joney.main.analysis.DateComparison;
import joney.main.analysis.TotalExpense;
import joney.main.utils.FormatDecimal;
import joney.main.utils.JoneyUtil;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;

public class BreakdownView extends ViewPart {

	public static String ID = "joney.breakdownview";
	
	private Table t;
	
	
	public void createPartControl(Composite parent) {
		Composite mainSplit = new Composite(parent, SWT.None);
		mainSplit.setLayout(new GridLayout(1, false));
		
		
		 t = new Table(mainSplit, SWT.BORDER);

		 TableColumn tc1 = new TableColumn(t, SWT.LEFT);
		 TableColumn tc2 = new TableColumn(t, SWT.CENTER);
		 TableColumn tc3 = new TableColumn(t, SWT.RIGHT);
		 tc1.setText("Name");
		 tc2.setText("Total");
		 tc3.setText("Outflow %");
		 tc1.setWidth(150);
		 tc2.setWidth(100);
		 tc3.setWidth(80);
		 t.setHeaderVisible(true);
		 t.setFont(new Font(JoneyUtil.getDisplay(), "Courier New", 11, SWT.NORMAL));
		 
		 
		//set layout for video frame
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace= true;
		t.setLayoutData(gridData);
		
		createTimeBar(mainSplit);
		
	}
	
	private void createTimeBar(Composite mainSplit)
	{
		//set top panel layout
		Composite topPanel = new Composite(mainSplit, SWT.BORDER);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		topPanel.setLayoutData(gridData);
		topPanel.setLayout(new GridLayout(4, false));
		
		
		Vector<Date> be = DateComparison.getMaxBeginAndEndDates();
		
		int begYear, endYear;
		if(be.get(0) == null || be.get(1) == null)
		{
			begYear = 2010;
			endYear = begYear;
		}
		else
		{
			begYear =be.get(0).getYear()+1900;
			endYear = be.get(1).getYear()+1900;
		}
		
		
		Label year = new Label(topPanel, SWT.None);
		year.setText("Year:");
		final Combo yearDrop = new Combo(topPanel, SWT.DROP_DOWN);
		for(int i=begYear; i <= endYear;i++)
		{
			yearDrop.add(String.valueOf(i));
		}
		yearDrop.select(yearDrop.getItemCount()-1);
		
		int yearb = Integer.valueOf(yearDrop.getText());
		
		Label month1 = new Label(topPanel, SWT.None);
		month1.setText("Month Begin:");
		final Combo monthDrop = new Combo(topPanel, SWT.DROP_DOWN);
		monthDrop.add("Jan");
		monthDrop.add("Feb");
		monthDrop.add("Mar");
		monthDrop.add("Apr");
		monthDrop.add("May");
		monthDrop.add("Jun");
		monthDrop.add("Jul");
		monthDrop.add("Aug");
		monthDrop.add("Sep");
		monthDrop.add("Oct");
		monthDrop.add("Nov");
		monthDrop.add("Dec");
		
		monthDrop.select(0);
		
		int monthb = monthDrop.getSelectionIndex();
		
		Label month2 = new Label(topPanel, SWT.None);
		month2.setText("Month End:");
		final Combo monthDrop2 = new Combo(topPanel, SWT.DROP_DOWN);
		monthDrop2.add("Jan");
		monthDrop2.add("Feb");
		monthDrop2.add("Mar");
		monthDrop2.add("Apr");
		monthDrop2.add("May");
		monthDrop2.add("Jun");
		monthDrop2.add("Jul");
		monthDrop2.add("Aug");
		monthDrop2.add("Sep");
		monthDrop2.add("Oct");
		monthDrop2.add("Nov");
		monthDrop2.add("Dec");
		
		monthDrop2.select(0);
		
		int monthe = monthDrop2.getSelectionIndex();
		
		
		Label day1 = new Label(topPanel, SWT.None);
		day1.setText("Day Begin:");
		final Combo dayDrop = new Combo(topPanel, SWT.DROP_DOWN);
		for(int i=1; i <= 31;i++)
		{
			dayDrop.add(String.valueOf(i));
		}
		
		dayDrop.select(0);
		
		int dayb = dayDrop.getSelectionIndex()+1;
		
		Label day2 = new Label(topPanel, SWT.None);
		day2.setText("Day End:");
		final Combo dayDrop2 = new Combo(topPanel, SWT.DROP_DOWN);
		for(int i=1; i <= 31;i++)
		{
			dayDrop2.add(String.valueOf(i));
		}
		
		dayDrop2.select(0);
		
		int daye = dayDrop2.getSelectionIndex()+1;
		
		
		
		updateData(yearb,0,1 ,yearb, 11, 1);
		
		yearDrop.add("All");
		
		Button show = new Button(topPanel, SWT.PUSH);
		show.setText("Show Dates");
		show.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e) {
			
				if(yearDrop.getText().equals("All"))
				{
					updateData(Integer.valueOf(yearDrop.getItem(0)),0,1,
							Integer.valueOf(yearDrop.getItem(yearDrop.getItemCount()-2)), 0,1
							);
				}
				else{
					int val = Integer.valueOf(yearDrop.getText());
					updateData(val,monthDrop.getSelectionIndex(), dayDrop.getSelectionIndex()+1, 
							val,monthDrop2.getSelectionIndex(), dayDrop2.getSelectionIndex()+1);
				}
			}
			
		});
	}

	protected void updateData(int yearb, int monthb, int dayb, int yeare, int monthe, int daye)
	{
		t.removeAll();
		
		Hashtable<String, TotalExpense> values = DateComparison.getAllTypeAmount(yearb,monthb, dayb, yeare, monthe, daye);
		 Vector<String> names = new Vector<String>();
		 for(String s: values.keySet())
			 names.add(s);
			 
		 Collections.sort(names);
		 for(String s: names)
		 {
			    TableItem item1 = new TableItem(t, SWT.NONE);
			    
			    
			    TotalExpense te = values.get(s);
			    item1.setText(new String[] { s, 
			    		String.valueOf(FormatDecimal.format(2, te.total)), 
			    		String.valueOf(FormatDecimal.format(2, te.percent))+"%" });
		 }
	}
	
	public void setFocus() {
		t.setFocus();
	}

}
